import numpy as np

def ax_b(logits, labels):
	probs, hard_labels = get_probs_and_labels(logits, labels)

	# print('origin_val_f1:{}, origin_val_p:{}, origin_val_r:{}'.format(*caculate_fpr(labels, probs)))
	# print('origin_test_f1:{}, origin_test_p:{}, origin_test_r:{}'.format(*caculate_fpr(labels, probs)))

	cost_matrix = np.dot(np.mat(hard_labels).T, np.linalg.pinv(np.mat(probs)).T).T
	return cost_matrix


def get_probs_and_labels(logits, labels):
	# datas = np.zeros(shape=(len(logits), len(logits[0])))
	datas = []
	hard_labels = []		# one-hot的硬编码
	# softmax原始概率
	for i in range(len(logits)):
		datas.append(np.exp(logits[i]) / np.sum(np.exp(logits[i])))
		# for j in range(len(logits[i])):
			# datas[i][j] = 1.0/(1+np.exp(-logits[i][j]))
	# datas = logits
	developer_size = len(logits[1])
	hard_labels = np.zeros(shape=(len(labels), developer_size))        # shape=(n_samples, n_developers)
	for i in range(len(labels)):
		hard_labels[i][int(labels[i])] = 1.0
	# print(hard_labels[0])
	# hard_labels = 1 - hard_labels	# 变取最大为取最小
	# hard_labels = (1 - hard_labels)*100	# 变取最大为取最小
	# hard_labels = (-1) * hard_labels
	# print(hard_labels[0])
	return datas, hard_labels

